package com.design.working.controller;

import com.design.working.config.BaseServlet;
import com.design.working.dto.counselor.CounselorHandleStudentApplicationDTO;
import com.design.working.dto.PageControlDTO;
import com.design.working.dto.counselor.SearchCounselorStudentAttendanceDTO;
import com.design.working.service.CounselorService;
import com.design.working.service.impl.CounselorServiceImpl;
import com.google.gson.Gson;
import org.apache.log4j.Logger;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * 辅导员端控制器
 *
 * @author ld
 */
@WebServlet(name = "CounselorController",
        urlPatterns = {"/counselor/*"})
public class CounselorController extends BaseServlet {

    private final CounselorService counselorService = new CounselorServiceImpl();
    Logger log = Logger.getLogger(UserController.class);

    /**
     * 没有参数, "0"
     */
    private static final String NO_PARAM = "0";

    /**
     * （事务审批）查找本院学生岗位申请记录
     */
    public void selectStudentWorkApplication (HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException{

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        HttpSession session = request.getSession();
        Gson gson = new Gson();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        response.getWriter().print(gson.toJson(counselorService.selectStudentWorkApplication(uid, pageControlDTO)));
    }

    /**
     * （事务审批）学生岗位申请 - 插入辅导员对学生申请记录的审批内容
     */
    public void handleStudentWorkApplication(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 接收前端管理员填写的审批信息
        CounselorHandleStudentApplicationDTO counselorCheckApplicationDTO = new CounselorHandleStudentApplicationDTO(
                                                                      Integer.valueOf(request.getParameter("id")),
                                                                      request.getParameter("advice"),
                                                                      Integer.valueOf(request.getParameter("operation")));

        counselorService.insertStudentApplicationHanding(uid, counselorCheckApplicationDTO);
        // 这里随意返回一个值，用于ajax的成功调用（因为那边不涉及操作返回参数）
        response.getWriter().print(1);
    }

    /**
     * （事务审批）学生岗位申请 - 查找申请表中学生申请工作填写的个人信息
     */
    public void selectStudentApplicationDetail(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        Gson gson = new Gson();

        // 获取学生信息id
        Integer smtId = Integer.valueOf(request.getParameter("smtId"));

        response.getWriter().print(gson.toJson(counselorService.selectStudentApplicationDetail(smtId)));
    }

    /**
     * 查找全部岗位
     */
    public void selectJob(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        Gson gson = new Gson();

        String place = request.getParameter("place");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        response.getWriter().print(gson.toJson(counselorService.selectJob(place, pageControlDTO)));
    }

    /**
     * 查找本院学生信息
     */
    public void selectStudentInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        HttpSession session = request.getSession();
        Gson gson = new Gson();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        response.getWriter().print(gson.toJson(counselorService.selectStudentInfo(uid, pageControlDTO)));
    }

    /**
     * 通过学号姓名查找本院学生信息
     */
    public void selectStudentInfoByName(HttpServletRequest request, HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/javascript; charset=UTF-8");
        HttpSession session = request.getSession();
        Gson gson = new Gson();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        // 空值转换
        String stuUid = "".equals(request.getParameter("uid")) ? NO_PARAM : request.getParameter("uid");
        String studentName = request.getParameter("studentName");

        response.getWriter().print(gson.toJson(counselorService.selectStudentInfoByName(uid, pageControlDTO, stuUid, studentName)));
    }


    /**
     * 查询学生详细信息
     */
    public void selectStudentMsg(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        Gson gson = new Gson();

        // 获取学生学号
        Integer uid = Integer.valueOf(request.getParameter("uid"));

        response.getWriter().print(gson.toJson(counselorService.selectStudentMsg(uid)));
    }

    /**
     * 查找本院学生的考勤记录
     */
    public void selectCollegeStudentAttendance(HttpServletRequest request, HttpServletResponse response) throws IOException{

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        HttpSession session = request.getSession();
        Gson gson = new Gson();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        response.getWriter().print(gson.toJson(counselorService.selectCollegeStudentAttendance(uid, pageControlDTO)));
    }

    /**
     * 通过学号姓名查询本院学生考勤记录
     */
    public void selectCollegeStudentAttendanceByLimit(HttpServletRequest request, HttpServletResponse response) throws IOException{

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        HttpSession session = request.getSession();
        Gson gson = new Gson();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        // 空值转换
        String stuUid = "".equals(request.getParameter("uid")) ? NO_PARAM : request.getParameter("uid");

        SearchCounselorStudentAttendanceDTO searchDTO = new SearchCounselorStudentAttendanceDTO();
        searchDTO.setId(stuUid);
        searchDTO.setName(request.getParameter("studentName"));
        searchDTO.setBeginDate(request.getParameter("beginDate"));
        searchDTO.setEndDate(request.getParameter("endDate"));

        response.getWriter().print(gson.toJson(counselorService.selectCollegeStudentAttendanceByLimit(uid, pageControlDTO, searchDTO)));
    }

    /**
     * 查找本院学生的岗位申请记录
     */
    public void selectStudentApplication(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        HttpSession session = request.getSession();
        Gson gson = new Gson();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        response.getWriter().print(gson.toJson(counselorService.selectStudentApplication(uid, pageControlDTO)));
    }

    /**
     * 通过学号姓名查询本院学生申请岗位的信息
     */
    public void selectStudentApplicationByName(HttpServletRequest request, HttpServletResponse response) throws IOException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/javascript; charset=UTF-8");
        HttpSession session = request.getSession();
        Gson gson = new Gson();

        // 获取辅导员工号
        Integer uid = (Integer) session.getAttribute("uid");
        // 获取分页参数
        PageControlDTO pageControlDTO = new PageControlDTO();
        pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
        pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

        // 空值转换
        String stuUid = "".equals(request.getParameter("uid")) ? NO_PARAM : request.getParameter("uid");
        String studentName = request.getParameter("studentName");

        response.getWriter().print(gson.toJson(counselorService.selectStudentApplicationByName(uid, pageControlDTO, stuUid, studentName)));
    }

    /**
     * 查询学生岗位申请的处理信息
     */
    public void selectStuAppExamination(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException {

        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        Gson gson = new Gson();

        Integer swaId = Integer.valueOf(request.getParameter("swaId"));
        response.getWriter().print(gson.toJson(counselorService.selectStuAppExamination(swaId)));
    }
}
